package com.css.dzj.dzjap.modules.project.supervision.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.annotation.SysLog;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.common.util.SnowflakeId;
import com.css.dzj.dzjap.modules.project.supervision.entity.Supervision;
import com.css.dzj.dzjap.modules.project.supervision.entity.SupervisionChild;
import com.css.dzj.dzjap.modules.project.supervision.service.SupervisionChildService;
import com.css.dzj.dzjap.modules.project.supervision.service.SupervisionService;
import com.css.dzj.dzjap.modules.project.supervision.vo.SupervisionList;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 监管工作
 *
 * @author liyuan
 */
@RequestMapping("/supervision")
@RestController
public class SupervisionController {
    @Autowired
    private SupervisionService supervisionService;
    @Autowired
    private SupervisionChildService supervisionChildService;
    @Autowired
    BaseService baseService;
    /**
     * 多条件分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param projectName
     * @param projectType
     * @param suType
     * @param checkStatus
     * @param reportStatus
     * @param suCreateUser
     * @param suTime
     * @param projectAddress 现在存的是用户组织地址、用它来判断
     * @return
     */
    /*@SysLog(value = "开展监管工作:分页")*/
    @GetMapping("/getPage")
    public R getPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                     @RequestParam(name = "projectName", required = false) String projectName,
                     @RequestParam(name = "projectType", required = false) String projectType,
                     @RequestParam(name = "suType", required = false) String suType,
                     @RequestParam(name = "checkStatus", required = false) String checkStatus,
                     @RequestParam(name = "reportStatus", required = false) String reportStatus,
                     @RequestParam(name = "suCreateUser", required = false) String suCreateUser,
                     @RequestParam(name = "suTime", required = false) String suTime,
                     @RequestParam(name = "projectAddress", required = false) String projectAddress,
                     @RequestParam(name = "orgLevel", required = false) String orgLevel) {
        try {
            // 根据用户组织地址判断权限
            if (StringUtils.isEmpty(projectAddress))
                return R.error("您没有权限！");
            PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
            QueryResults<Supervision> page = supervisionService.getPage(projectName, projectType, suType,
                    checkStatus, reportStatus, suCreateUser, suTime, projectAddress, orgLevel,
                    pageRequest);
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("data", page);
        } catch (Exception e) {
            baseService.logFailed(3,"查询失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据父ID获取主表和子表的信息
     * @param Id
     * @return
     */
   /* @SysLog(value = "开展监管工作:获取主表与子表信息")*/
    @GetMapping("/getSupervisionList")
    public R getSupervisionList(@RequestParam String Id) {
        try {
            List<SupervisionChild> supervisionChildren = supervisionChildService.queryByParentId(Id);
            Supervision supervision = supervisionService.queryById(Id);
            SupervisionList supervisionList = new SupervisionList();
            supervisionList.setSupervisionChildren(supervisionChildren);
            supervisionList.setSupervision(supervision);
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("data", supervisionList);
        } catch (Exception e) {
            baseService.logFailed(3,"查询失败","2");
            return R.error(e.getMessage());
        }
    }
    /**
     * 根据ID查看主表详情---旧接口
     *
     * @param
     * @return
     */
   /* @SysLog(value = "开展监管工作:根据ID获取主表信息")*/
    @GetMapping("/getInfo")
    public R getSupervisionById(@RequestParam("Id") String reId) {
        try {
            Supervision supervision = supervisionService.queryById(reId);
            baseService.logSuccess(3,"获取成功","2");
            return R.ok().put("data", supervision);
        } catch (Exception e) {
            baseService.logFailed(3,"获取失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 3. 批量添加
     * 前台功能待实现
     *
     * @param supervisions
     * @return
     */
    /*@SysLog(value = "开展监管工作:批量添加")*/
    @PostMapping("/addlist")
    public R add(@RequestBody List<Supervision> supervisions) {
        try {
            supervisionService.save(supervisions);
            baseService.logSuccess(3,"获取成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"获取失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 单个新增
     *
     * @param supervisionList
     * @return
     */
   /* @SysLog(value = "开展监管工作:保存单个新增")*/
    @PostMapping("/add")
    @Transactional
    public R add(@RequestBody SupervisionList supervisionList) {
        try {
            /**
             * 主表提交新增
             */
            Supervision supervision = supervisionList.getSupervision();
            if (supervision == null){
                return R.error("主表为空");
            }
            supervision.setSuId(SnowflakeId.getId());
            // 设置审核状态为1：已提交
            supervision.setCheckStatus("1");
            supervisionService.save(supervision);
            /**
             * 子表提交新增
             */
            List<SupervisionChild> supervsionChildren = supervisionList.getSupervisionChildren();
            if (supervsionChildren == null){
                return R.error("子表为空");
            }
            for (SupervisionChild supervisionChild : supervsionChildren) {
                // 遍历子表list，设置父ID
                supervisionChild.setParentId(supervision.getSuId());
            }
            supervisionChildService.save(supervsionChildren);
            baseService.logSuccess(3,"提交成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"提交失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 暂存，设置监管结果状态为0
     *
     * @param supervisionList
     * @return
     */
    /*@SysLog(value = "开展监管工作:暂存")*/
    @PostMapping("/zcadd")
    public R zcadd(@RequestBody SupervisionList supervisionList) {
        try {
            /**
             * 主表提交新增
             */
            Supervision supervision = supervisionList.getSupervision();
            if (supervision == null){
                return R.error("主表为空");
            }
            supervision.setSuId(SnowflakeId.getId());
            // 设置审核状态为0：未提交
            supervision.setCheckStatus("0");
            supervisionService.save(supervision);
            /**
             * 子表提交新增
             */
            List<SupervisionChild> supervisionChildren = supervisionList.getSupervisionChildren();
            if (supervisionChildren == null){
                return R.error("子表为空");
            }
            for (SupervisionChild supervisionChild : supervisionChildren) {
                // 遍历子表list，设置父ID
                supervisionChild.setParentId(supervision.getSuId());
            }
            supervisionChildService.save(supervisionChildren);
            baseService.logSuccess(3,"暂存成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"暂存失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 4. 修改
     *
     * @param supervisionList
     * @return
     */
    /*@SysLog(value = "开展监管工作:修改未提交")*/
    @PostMapping("/update")
    public R update(@RequestBody SupervisionList supervisionList) {
        try {
            /**
             * 主表修改
             */
            Supervision supervision = supervisionList.getSupervision();
            // 修改后，若审核状态不为0，则置为默认的0：未提交
            if (!"0".equals(supervision.getCheckStatus()))
                supervision.setCheckStatus(Constant.CHECK_STATUS_TJ_NO);
            Supervision targetSupervision = supervisionService.queryById(supervision.getSuId());
            // 空的值不覆盖原有数据
            BeanUtil.copyBeanNotNullToBean(supervision, targetSupervision);
            supervisionService.update(targetSupervision);
            /**
             * 子表修改
             */
            List<SupervisionChild> supervsionChildren = supervisionList.getSupervisionChildren();
            supervisionChildService.update(supervsionChildren);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 开展监管工作:修改提交-->未用到的接口
     * @param supervisionList
     * @return
     */
    /*@SysLog(value = "开展监管工作:修改提交")*/
    @PostMapping("/updateSubmit")
    public R updateSubmit(@RequestBody SupervisionList supervisionList) {
        try {
            /**
             * 主表修改
             */
            Supervision supervision = supervisionList.getSupervision();
            // 修改后已提交待审核
            if ("0".equals(supervision.getCheckStatus()))
                supervision.setCheckStatus(Constant.CHECK_STATUS_TJ_YES);
            Supervision targetSupervision = supervisionService.queryById(supervision.getSuId());
            // 空的值不覆盖原有数据
            BeanUtil.copyBeanNotNullToBean(supervision, targetSupervision);
            supervisionService.update(targetSupervision);
            /**
             * 子表修改
             */
            List<SupervisionChild> supervsionChildren = supervisionList.getSupervisionChildren();
            supervisionChildService.update(supervsionChildren);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 开展监管工作:审定
     * 只更新主表的审定数据
     * @param supervisionList
     * @return
     */
    /*@SysLog(value = "开展监管工作:审核")*/
    @PostMapping("/check")
    public R check(@RequestBody SupervisionList supervisionList) {
        try {
            Supervision supervision = supervisionList.getSupervision();
            if (supervision == null){
                return R.error("主表为空");
            }
            Supervision targetSupervision = supervisionService.queryById(supervision.getSuId());
            // 空的值不覆盖原有数据
            BeanUtil.copyBeanNotNullToBean(supervision, targetSupervision);
            supervisionService.check(targetSupervision);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }

    }

    /**
     * 上报
     *
     * @param id
     * @return
     */
    /*@SysLog(value = "开展监管工作:上报")*/
    @PostMapping("/report/{id}")
    public R check(@PathVariable String id) {
        try {
            // 更改上报状态
            supervisionService.updateReportStatus(id);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 操作按钮：提交
     *
     * @param id
     * @return
     */
    /*@SysLog(value = "开展监管工作:操作按钮的提交")*/
    @PostMapping("/submit/{id}")
    public R submit(@PathVariable String id) {
        try {
            // 更改上报状态
            supervisionService.updateSubmitStatus(id);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 操作按钮：撤回
     *
     * @param id
     * @return
     */
    /*@SysLog(value = "开展监管工作:操作按钮撤回")*/
    @PostMapping("/recall/{id}")
    public R recall(@PathVariable String id) {
        try {
            // 更改上报状态
            supervisionService.recallSubmitStatus(id);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 主表和子表删除
     *
     * @param id
     * @return
     */
    /*@SysLog(value = "开展监管工作:主表和子表逻辑删除")*/
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        try {
            supervisionService.logicDel(id);
            baseService.logSuccess(3,"删除成功","2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3,"删除失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 开展监管工作
     * 中国地震局用户添加拟制公告
     *
     * @param apPublicEntity
     * @return
     */
    /*@SysLog("开展监管工作：拟制公告")*/
    @PostMapping("/addPublick")
    public R addPublick(@RequestBody ApPublicEntity apPublicEntity) {
        try {
            supervisionService.addPublick(apPublicEntity);
            baseService.logSuccess(3,"添加失败","2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3,"添加失败","2");
            return R.error(e.getMessage());
        }
    }


    /**
     * 开展监管工作：查看公告
     *
     * @param Id
     * @return
     */
    /*@SysLog("开展监管工作：查看公告")*/
    @GetMapping("/viewPublick")
    public R viewPublick(String Id) {
        try {
            ApPublicEntity apPublicEntity = supervisionService.viewPublickBySuId(Id);
            baseService.logSuccess(3,"查看成功","2");
            return R.ok().put("data", apPublicEntity);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3,"查看失败","2");
            return R.error(e.getMessage());
        }
    }
}
